﻿using Model;
using FairyGUI;
using System;
using DG.Tweening;
using UnityEngine;
using MDFGame;
namespace Hotfix
{
    [UIView(UIViewType.Game)]
    public class GameView : UIView
    {
        public override string PackageName => "GameUI";
        public override string ViewName => "GameView";


        GComponent card;
        GComponent Area;
        public override void Create()
        {

        }
        public override void OnEnter()
        {
            /*以下为UI测试代码，测试完毕删除*/
            
            card = UIPackage.CreateObject("cardUI","BattleCard").asCom;
            GComponent a1=MainUI.GetChild("Deck_P2").asCom;
            Area = a1;
            Area.AddEventListener("CardIn",(EventContext context)=>
            {
                Log.Info("卡片进入此处");
            });
            a1.AddChild(card);
            card.SetPivot(0, 0, true);
            card.SetXY(0,0);
            card.visible = true;
            GComponent a2 = MainUI.GetChild("Deck_P1").asCom;
            card.SetXY(GRoot.inst.LocalToGlobal(card.xy).x, GRoot.inst.LocalToGlobal(card.xy).y);
            GRoot.inst.AddChild(card);
            card.draggable = true;
            card.touchable = true;
            card.onDragStart.Add(OnDargS);
            card.onDragEnd.Add(OnDargE);
            /*
            Log.Info(card.xy.ToString());
            DOTween.To(
                ()=>card.xy,
                x=>card.xy=x,
                a2.xy,
                2f
                ).SetEase(Ease.Linear);*/
                
        }

        private void OnDargS()
        {
            card.touchable = false;
        }
        private void OnDargE()
        {
            /*用于释放卡牌时向目标区域发送消息*/
            GObject touch = GRoot.inst.touchTarget;
            while (touch != null)
            {
                if (touch.hasEventListeners("CardIn"))
                {
                    touch.RequestFocus();
                    touch.DispatchEvent("CardIn");
                    card.touchable = true;//记得设置回可触摸
                    return;
                }
                touch = touch.parent;
            }
            Log.Info("当前卡片移动终点无区域");
            card.touchable = true;//记得设置回可触摸
        }
    }

}

